package com.jandar.ds.service;

import com.jandar.ds.mapper.DbInfoPowerMapper;
import com.jandar.ds.pojo.DbInfoPower;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xiongxx
 * @date 2023/7/6 14:32
 * @description
 */
@Service
public class DbInfoPowerService {
    @Autowired
    DbInfoPowerMapper dbInfoPowerMapper;


    public void save(List<DbInfoPower> dbInfoPowers) {
        if (dbInfoPowers.isEmpty()) {
            return;
        }
        DbInfoPower first = dbInfoPowers.get(0);
        this.deleteByDbId(first.getDbInfoId());
        dbInfoPowers.forEach(dbInfoPower -> dbInfoPowerMapper.insert(dbInfoPower));
    }


    public List<Integer> listByPower(Integer userId, Integer roleId, Integer deptId) {
        DbInfoPower dbInfoPower = new DbInfoPower();
        dbInfoPower.setRoleId(roleId);
        dbInfoPower.setDeptId(deptId);
        dbInfoPower.setUserId(userId);
        final List<DbInfoPower> list = dbInfoPowerMapper.list(dbInfoPower);
        return list.stream().map(DbInfoPower::getDbInfoId).collect(Collectors.toList());
    }

    public List<DbInfoPower> listByDbInfoId(Integer id) {
        DbInfoPower dbInfoPower = new DbInfoPower();
        dbInfoPower.setDbInfoId(id);
        return dbInfoPowerMapper.list(dbInfoPower);
    }

    public void deleteByDbId(Integer id) {
        dbInfoPowerMapper.deleteByDbInfoId(id);
    }
}
